package com.cloudz.iptv.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.cloudz.iptv.api.v1.AliTssServiceApi;
import com.cloudz.iptv.audio.AliTss;
import com.cloudz.iptv.utils.InPutJson;
import com.cloudz.iptv.utils.ResponseUtils;
import com.cloudz.iptv.utils.RetrofitUtils;
import com.cloudz.iptv.utils.SimplePreference;

import cn.jpush.android.api.JPushInterface;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * @File Name: com.cloudz.iptv.push
 * @Author: YJ //作者及
 * @Date: 2019/3/17 12:33 AM//完成日期
 * @Description: // 自定义接收器
 * @Version: v0.0.1 // 版本信息
 * @Function List: // 主要函数及其功能
 * @Others: // 其它内容的说明
 * @History: // 历史修改记录
 */
public class CustomReceiver extends BroadcastReceiver
{

    private static final String TAG = "JIGUANG-Example";

    @Override
    public void onReceive(Context context, Intent intent)
    {

        Bundle bundle = intent.getExtras();
        Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: ");

        if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction()))
        {
            String msg = bundle.getString(JPushInterface.EXTRA_MESSAGE);

            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + msg);

            AliTssServiceApi aliTssServiceApi = RetrofitUtils.getInstance().createApi(SimplePreference.getServerUrl(context), AliTssServiceApi.class);
            aliTssServiceApi.getAliYyToken().enqueue(new Callback<ResponseBody>()
            {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
                {

                    try
                    {
                        InPutJson inOutJson = ResponseUtils.getInOutJson(response);

                        if (inOutJson.result)
                        {
                            String data = inOutJson.data;

                            AliTss.getInstance().startSynthesizer(data, msg);

                        }
                    } catch (Exception e)
                    {
                        Log.d("homePage----->", "获取首页数据失败");


                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t)
                {

                }
            });
        }
    }
}
